'use strict'
var _ = require('lodash')

function setProductAddress() {
  this.oauthAddress = 'open-api.shop.ele.me'
  this.apiAddress = 'open-api.shop.ele.me'
}

function setSandboxAddress() {
  this.oauthAddress = 'open-api-sandbox.shop.ele.me'
  this.apiAddress = 'open-api-sandbox.shop.ele.me'
}

function setAddress() {
  if (this.sandbox) {
    setSandboxAddress.call(this)
  } else {
    setProductAddress.call(this)
  }
}

class Config {
  constructor(opt) {
    if (opt) {
      if (!opt.key) {
        throw new Error('app key can not be empty')
      }
      if (!opt.secret) {
        throw new Error('app secret can not be empty')
      }
      if (!_.isBoolean(opt.sandbox)) {
        throw new Error('sandbox want to be Boolean value')
      }
      this.sandbox = opt.sandbox
      this.key = opt.key
      this.secret = opt.secret
      setAddress.call(this)
    } else {
      throw new Error("can't init Config without options")
    }
  }

  setKey(key) {
    this.key = key
  }

  setSecret(secret) {
    this.secret = secret
  }

  setSandbox(isSandBox) {
    this.sandbox = isSandBox
    setAddress.call(this)
  }

  setOauthAddress(oauthAddress) {
    this.oauthAddress = oauthAddress
  }

  setApiAddress(apiAddress) {
    this.apiAddress = apiAddress
  }

  getKey() {
    return this.key
  }

  getSecret() {
    return this.secret
  }

  getIsSandbox() {
    return this.sandbox
  }

  getOauthAddress() {
    return this.oauthAddress
  }

  getApiAddress() {
    return this.apiAddress
  }

  setLog(log) {
    if (_.isEmpty(log)) {
      throw new Error('log can not be empty')
    }

    // 判断是否有info和warn两个方法
    if (!_.isFunction(log.info)) {
      throw new Error("cant't set log without method of info")
    }

    if (!_.isFunction(log.error)) {
      throw new Error("cant't set log without method of error")
    }

    this.log = log
  }

  getLog(log) {
    return this.log
  }
}

module.exports = Config